package zookeeper.client;

import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

@Slf4j
public class ClusterOperations extends ClusterBase {

  @Test
  public void testReconnect() throws InterruptedException {
    while (true) {
      try {
        Stat stat = new Stat();
        byte[] data = getZooKeeper().getData("/zookeeper", false, stat);
        log.info("get data : {}", new String(data));

        TimeUnit.SECONDS.sleep(5);
      } catch (InterruptedException | KeeperException e) {
        e.printStackTrace();
        log.info("开始重连......");
        while (true) {
          log.info("zookeeper status :{}", getZooKeeper().getState().name());
          if (getZooKeeper().getState().isConnected()) {
            break;
          }
          TimeUnit.SECONDS.sleep(3);
        }
      }
    }
  }
}
